
/**
 * OSG. Important classes to 
 */

var osg = function() {};

osg.$ = function( elId ) {
	return document.getElementById(elId);
}


/**
 * A short function to 'document.createElement'
 * 
 */
osg.E = function( elId ) {
	return document.createElement(elId);
}

/**
 * A short function to 'document.createTextNode'
 * 
 */
osg.T = function( v ) {
	return document.createTextNode(osg.decode(v));
}

osg.decode = function(s) {
	var e = document.createElement("div");
	e.innerHTML = s;
	return e.innerHTML;
}

/**
 * Some code to create <img> tags with google.proxy if that exists.
 */
osg.imgProxy = function( url ) {
	var img = document.createElement("img");
	img.src = url;
	return img;
}


/**
 * Template Object.
 * @param template name - String
 */
osg.Template = function( name ) {
	this._data = {};
	var name = name.split(".")[0].toLowerCase();		
	var f = osg.Template._template(name);
	if ( typeof f == 'function' ) {
		this._f = gadgets.util.makeClosure(this, f);		
	} else {
		alert("[ERRO] template " + name + " ");
	}	
}

/**
 * Templates registereds.
 */
osg.Template._templates = [];


/**
 * A short function to "Template.get";
 */
osg.Template.prototype.V = function(k) {
	return this.get(k);
}

osg.Template.prototype.get = function(k) {
	return this._data[k];
}

osg.Template.prototype.keys = function() {
	var list = [];
	for( var i in this._data) {
		if ( typeof this._data[i] == 'function' || i.substr(i.length - 3, 3) == "___" ) continue;
		list.push(i);
	}
	return list;	
}

osg.Template.prototype.append = function(template) {
	this._data = {};
	var name = template.split(".")[0].toLowerCase();		
	var f = osg.Template._template(name);
	if ( typeof f == 'function' ) {
		gadgets.util.makeClosure(this, f)();		
	} else {
		alert("[ERRO] template " + name + " ");
	}	
}

osg.Template.prototype.add = function(k, v) {
	this._data[k] = v;
}

osg.Template.prototype.dom = function() {
	return this._f();
}


osg.Template.prototype.show = function(divId) {
	osg.setPage(this.dom(), divId);
}

/**
 * Returns a value as "osg.Interable" Object.
 */
/*osg.TemplateData.prototype.A = function(k) {
	return new osg.Interable(this._data[k]);
}*/

/**
 * Returns template function
 */
osg.Template._template = function( key ) {
	return this._templates[key];
}

/**
 * Register a template function
 */
osg.Template.register = function(name, func) {
	this._templates[name] = func;	
};


osg.Interable = function( va ) {
	this._ = va;
}
osg.Interable.prototype.each = function( iterator ) {
	for( var i in this._ ) {
		if ( typeof this._[i] == 'function' || i.substr(i.length - 3, 3) == "___" ) continue;
		iterator(this._[i], i);		
	}
}

/**
 * Show page
 */
osg.setPage = function( page, elId ) {
	var el = document.getElementById((elId)?elId:'app__MODULE_ID__');
	if ( typeof page == "string" ) {
    	el.innerHTML = html;
	} else {
    	el.innerHTML = "";
    	el.appendChild( page );
	}
}

/**
 * Register a application.
 */
osg.registerApp = function( instance ) {
	if ( instance ) {
		if ( typeof instance.onCreate == 'function' ) {
			gadgets.util.registerOnLoadHandler(gadgets.util.makeClosure(instance, instance.onCreate));
		} else {
			//TODO: error.
		}
	}
}


if( typeof gadgets != "undefined" &&  typeof gadgets.util != "undefined" && typeof gadgets.util.registerOnLoadHandler == "function" ) {
	//$INSTANCE._surface = gadgets.views.getCurrentView();
	gadgets.util.registerOnLoadHandler(gadgets.util.makeClosure($INSTANCE, $INSTANCE.$initMethod));
} else {

}


